\chapter{Android}
\label{programmingAndroid}

\section{Introduction}

The information presented in this chapter has been extracted mainly from the books \cite{androidBegginer} and 
\cite{androidProfessional} and from the web \cite{androidWS}. Based on our experience with Android we have compiled a small 
guide that will give the reader an idea of the operations and methods of developing applications for the Android platform. 
However, in order to continue development of our application is highly recommended to consult the bibliography provided above.

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. The 
Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java 
programming language.

Android sits alongside a new wave of mobile operating systems designed for increasingly powerful mobile hardware. Windows 
Mobile, the Apple iPhone, and the Palm Pre now provide a richer, simplified development environment for mobile applications. 
However, unlike Android, they're built on proprietary operating systems that in some cases prioritize native applications over 
those created by third parties, restrict communication among applications and native phone data, and restrict or control the 
distribution of third-party apps to their platforms. Android offers new possibilities for mobile applications by offering an 
open development environment built on an open-source Linux kernel. Hardware access is available to all applications through a 
series of API libraries, and application interaction, while carefully controlled, is fully supported.

\section{Android architecture}

The following diagram shows the major components of the Android operating system. Each section is described in more detail below.

\begin{figure}[ht]
\centering
\includegraphics[width=1\textwidth ]{./Appendices/images/system-architecture.jpg} 
\caption{Android architecture}
\end{figure}

\newpage

\section{Android SDK}
The Android software development kit (SDK) includes everything you need to start developing, testing, and debugging Android 
applications. Included in the SDK download are:
\begin{enumerate}
    \item \emph{The Android APIs:} The core of the SDK is the Android API libraries that provide developer access to the 
	  Android stack. These are the same libraries used at Google to create native Android applications.
    \item \emph{Development tools:} So you can turn Android source code into executable Android applications, the SDK 
	  includes several development tools that let you compile and debug your applications.
    \item \emph{The Android Virtual Device Manager and Emulator:} The Android Emulator is a fully interactive 
    Android device emulator featuring several alternative skins. The emulator runs within an Android Virtual Device 
    that simulates the device hardware configuration. Using the emulator you can see how your applications will look 
    and behave on a real Android device. All Android applications run within the Dalvik VM, so the software emulator is 
    an excellent environment, in fact, as it is hardware-neutral, it provides a better independent test environment than 
    any single hardware implementation. You can see an example of the Emulator at \emph{Figure \ref{avd}},

  \begin{figure}[ht]
  \centering
  \includegraphics[width=1\textwidth ]{./Appendices/images/avd.png} 
  \caption{Android Emulator}
  \label{avd}
  \end{figure}
 
    \item \emph{Full documentation:} The SDK includes extensive code-level reference information detailing exactly what's 
    included in each package and class and how to use them. In addition to the code documentation, Android's reference 
    documentation explains how to get started and gives detailed explanations of the fundamentals behind Android development.    
    \item \emph{Sample code:} The Android SDK includes a selection of sample applications that demonstrate some of the 
    possibilities available with Android, as well as simple programs that highlight how to use individual API features.    
    \item \emph{Online support:} Android has rapidly generated a vibrant developer community. The Google Groups at 
    \cite{googleGroups} are active forums of Android developers with regular input from the Android engineering and developer
    relations teams at Google.
\end{enumerate}

\section{Application components}
Android applications consist of loosely coupled components, bound by an application manifest that describes each component and 
how they all interact, as well as the application metadata including its hardware and platform requirements.

The following seven components provide the building blocks for your applications:
\begin{enumerate}
  \item \emph{Activities:} Your application's presentation layer. Every screen in your application will be an extension of the 
  Activity class. Activities use Views to form graphical user interfaces that display information and respond to user actions.
  \item \emph{Services:} The invisible workers of your application. Service components run in the background, updating your 
  data sources and visible Activities and triggering Notifications. They're used to perform regular processing that needs to 
  continue even when your application's Activities aren't active or visible.
  \item \emph{Content Providers:} Shareable data stores. Content Providers are used to manage and share application databases. 
  They're the preferred means of sharing data across application boundaries. This means that you can configure your own 
  Content Providers to permit access from other applications and use Content Providers exposed by others to access their stored 
  data.   
  \item \emph{Intents:} An inter-application message-passing framework. Using Intents you can broadcast messages system-wide or to 
  a target Activity or Service, stating your intention to have an action performed. The system will then determine the target(s) 
  that will perform any actions as appropriate.
  \item \emph{Broadcast Receivers:} Intent broadcast consumers. If you create and register a Broadcast Receiver, your application 
  can listen for broadcast Intents that match specific filter criteria. Broadcast Receivers will automatically start your 
  application to respond to an incoming Intent, making them perfect for creating event-driven applications.

  \item \emph{Widgets Visual:} application components that can be added to the home screen. A special variation of a Broadcast 
  Receiver, widgets let you create dynamic, interactive application components for users to embed on their home screens.

  \item \emph{Notifications:} A user notification framework. Notifications let you signal users without stealing focus or 
  interrupting their current Activities. They're the preferred technique for getting a user's attention from within a Service 
  or Broadcast Receiver.
\end{enumerate}

\section{Project structure}

The Android build system is organized around a specific directory tree structure for your Android project, much like any other 
Java project. The specifics, though, are fairly unique to Android. When you create a new Android project you get several items 
in the project's root directory, including the following:
\begin{enumerate}
 \item \textbf{AndroidManifest.xml:} An XML file describing the application being built and which components, activities, services, 
  and so on are being supplied by that application. Underneath the manifest element, you may find the following elements: 
  \begin{enumerate}
    \item \emph{uses-permission elements:} Indicate the permissions your application will need in order to function properly.
    \item \emph{permission elements:} Declare permissions that activities or services might require other applications to hold 
    in order to use your application's data or logic.
    \item \emph{instrumentation elements:} Indicate code that should be invoked on key system events, such as starting up 
    activities, for the purposes of logging or monitoring.
    \item \emph{uses-library elements:} Hook in optional Android components, such as mapping services.
    \item \emph{uses-sdk element:} Indicates for which version of the Android SDK the application was built.
    \item \emph{application element:} Defines the guts of the application that the manifest describes.
  \end{enumerate}
  As an example you can view this AndroidManifest.xml

  \vspace{0.5cm}
  \begin{lstlisting}[captionpos=b,label=androidmanifest, caption={\textit{AndroidManifest.xml}},language=XML, 
  basicstyle=\small, frame=single]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=
  "http://schemas.android.com/apk/res/android"
  package="com.ffandroid"
  android:versionCode="1"
  android:versionName="1.0">
  <uses-permission 
    android:name="android.permission.INTERNET" />
  <uses-permission android:name=
    "android.permission.ACCESS_FINE_LOCATION" />
  <application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name">
    <uses-library 
      android:name="com.google.android.maps" />   	

      <activity android:name=".ffandroid"
	android:label="@string/app_name">
        <intent-filter>
	  <action android:name=
	    "android.intent.action.MAIN" />
	    <category 
	      android:name=
	      "android.intent.category.LAUNCHER"/>
        </intent-filter>
      </activity>

      <activity android:name=".UsersMap"
	android:label="@string/app_name">
        <intent-filter>
	  <action android:name=
	    "android.intent.action.MAIN"/>
	  <category android:name=
	    "android.intent.category.LAUNCHER"/>
	</intent-filter>
      </activity>     

      <activity android:name=".GPS"
	android:label="@string/app_name">
	<intent-filter>
	  <action android:name=
	    "android.intent.action.MAIN" />
	  <category android:name=
	    "android.intent.category.LAUNCHER"/>
	</intent-filter>
      </activity>    

    </application>

</manifest> 
\end{lstlisting}

 \item \textbf{Build.xml:} An Ant script for compiling the application and installing it on the device.
 \item \textbf{default.properties and local.properties:} Property files used by the Ant build script.
 \item \textbf{assets/:} A folder that holds other static files you wish packaged with the application for deployment 
	  onto the device.
 \item \textbf{bin/:} A folder that holds the application once it is compiled.

  \begin{enumerate}
     \item \emph{bin/classes/:} Holds the compiled Java classes.
     \item \emph{bin/classes.dex:} Holds the executable created from those compiled Java classes.
     \item \emph{bin/yourapp.apk:} Holds your application's resources, packaged as a ZIP file (where yourapp is the name 
	    of your application).
     \item \emph{bin/yourapp-debug.apk or bin/youapp- unsigned.apk:} The actual Android application (where yourapp is the 
	    name of your application).
  \end{enumerate}

  \item \textbf{gen/:} Where Android's build tools will place source code that they generate.
  \item \textbf{libs/:} A folder that holds any third-party JARs your application requires.
  \item \textbf{src/:} A folder that holds the Java source code for the application.
  \item \textbf{res/:} A folder that holds resources (such as icons, graphic user interface layouts, and the like) that are 
	packaged with the compiled Java in the application.

  \begin{enumerate}
    \item \emph{res/drawable/:} For images (PNG, JPEG, etc.).
    \item \emph{res/layout/:} For XML-based UI layout specifications.
    \item \emph{res/menu/:} For XML-based menu specifications.
    \item \emph{res/raw/:} For general-purpose files (e.g., a CSV file of account information).
    \item \emph{res/values/:} For strings, dimensions, and the like.
    \item \emph{res/xml/:} For other general-purpose XML files you wish to ship.
  \end{enumerate}

  \item \textbf{tests/:} A folder that holds an entirely separate Android project used for testing the one you created.

\end{enumerate}